/**
 * Created by 1 on 2016/7/5.
 */
Ext.define('Monkey.view.code.Generator', {
    extend: 'Ext.panel.Panel',
    xtype: 'codegenerator',
    requires: [
        'Ext.tree.Panel'
    ],

    id: 'codegenerator',
    cls: 'mk-generator',
    border: false,
    viewModel: 'vm-generator',
    controller: 'ctrl-generator',
    layout: {
        type: 'vbox',
        align: 'stretch'
    },
    buttonAlign: 'center',
    buttons: [{
        icon: 'resources/icons/16/refresh.png',
        text: '开始生成代码',
        handler: 'startGenerate'
    }],
    initComponent: function () {
        var modelPanel = {
            title: '配置通用参数',
            icon: 'resources/icons/16/drink.png',
            flex: 1,
            layout: {
                type: 'hbox',
                align: 'stretch'
            },
            items: [{
                flex: 2,
                bodyStyle: 'border-width:1px 0 0 0',
                xtype: 'form',
                layout: 'form',
                reference: 'modelParamConfig',
                defaults: {
                    labelWidth: 80,
                    labelAlign: 'right'
                },
                //tbar: [{
                //    icon: 'add2',
                //    text: '导入已存在的Model'
                //}],
                defaultType: 'textfield',
                items: [{
                    name: 'basePath',
                    allowBlank: false,
                    emptyText: '存放Controller,Model等文件的路径',
                    fieldLabel: '<span style="color: red;">*</span>JAVA基路径',
                    value: 'com/monkey/web/modules'
                }, {
                    name: 'moduleName',
                    emptyText: '模块名称，和JAVA基路径构成包地址',
                    allowBlank: false,
                    fieldLabel: '<span style="color: red;">*</span>模块名'
                }, {
                    name: 'className',
                    emptyText: '生成的实体类名',
                    allowBlank: false,
                    fieldLabel: '<span style="color: red;">*</span>Domain类名'
                }, {
                    name: 'tableName',
                    emptyText: '数据库表名',
                    allowBlank: false,
                    fieldLabel: '<span style="color: red;">*</span>表名'
                }, {
                    name: 'superClass',
                    emptyText: '选择继承的父类',
                    allowBlank: false,
                    fieldLabel: '<span style="color: red;">*</span>父类',
                    xtype: 'combobox',
                    queryMode: 'local',
                    displayField: 'name',
                    valueField: 'value',
                    value: 'BaseEntity',
                    store: Ext.create('Ext.data.Store', {
                        fields: ['name', 'value'],
                        data: [
                            {"name": "BaseEntity", "value": "BaseEntity"}
                        ]
                    })
                }, {
                    emptyText: 'Domain类说明信息',
                    name: 'remark',
                    xtype: 'textareafield',
                    grow: true,
                    fieldLabel: '备注'
                }/*, {
                 name: 'interface',
                 emptyText: '选择现实的接口',
                 fieldLabel: '接口',
                 xtype: 'checkboxgroup',
                 columns: 3,
                 vertical: true,
                 items: [
                 {boxLabel: 'Treeable', name: 'interface', inputValue: 'Treeable'},
                 {boxLabel: 'LogicDeleteable', name: 'interface', inputValue: 'LogicDeleteable'}
                 ]
                 }*/]
            }, {
                flex: 2,
                xtype: 'grid',
                id: 'modelPropertyGrid',
                reference: 'modelPropertyGrid',
                title: 'Domain类的基本属性<span style="color: red;">(至少添加一个)</span>',
                icon: 'resources/icons/16/table.png',
                columnLines: true,
                store: Ext.create('Ext.data.Store'),
                selType: 'checkboxmodel',
                bodyStyle: 'border-width:1px 1px 0 1px',
                tbar: [{
                    icon: 'resources/icons/16/add2.png',
                    text: '添加',
                    handler: 'showPropertiesWindow'
                }, '-', {
                    icon: 'resources/icons/16/delete.png',
                    text: '删除',
                    handler: 'removeProperty'
                }],
                columns: [{
                    width: 30,
                    xtype: 'rownumberer'
                }, {
                    text: '属性名',
                    sortable: true,
                    flex: 1,
                    dataIndex: 'name'
                }, {
                    text: '属性类型',
                    dataIndex: 'type',
                    flex: 1
                }, {
                    text: '备注',
                    dataIndex: 'remark',
                    flex: 2
                }]

            }, {
                flex: 1,
                xtype: 'form',
                title: '选择生成哪些文件<span style="color: red;">(Domain必选)',
                bodyStyle: 'border-width:1px 0 0 0',
                icon: 'resources/icons/16/check_error.png',
                reference: 'checkParamConfig',
                layout: 'form',
                defaults: {
                    labelWidth: 80,
                    labelAlign: 'right'
                },
                items: [{
                    labelSeparator: '',
                    value: true,
                    readOnly: true,
                    name: 'isDomain',
                    xtype: 'checkboxfield',
                    boxLabel: 'Domain'
                }, {
                    labelSeparator: '',
                    value: true,
                    name: 'isController',
                    xtype: 'checkboxfield',
                    boxLabel: 'Controller'
                }, {
                    labelSeparator: '',
                    value: true,
                    name: 'isService',
                    xtype: 'checkboxfield',
                    boxLabel: 'Service'
                }, {
                    labelSeparator: '',
                    value: true,
                    name: 'isRepository',
                    xtype: 'checkboxfield',
                    boxLabel: 'Repository'
                }, {
                    labelSeparator: '',
                    value: true,
                    name: 'isView',
                    xtype: 'checkboxfield',
                    boxLabel: 'View'
                }]
            }]
        };

        var tplPanel = {
            title: '选择模版以及配置',
            icon: 'resources/icons/16/bullet_lightning.png',
            flex: 1,
            layout: {
                type: 'hbox',
                align: 'stretch'
            },
            items: [{
                flex: 1,
                xtype: 'grid',
                title: '模版列表',
                columnLines: true,
                bodyStyle: 'border-width:1px 1px 0 0',
                reference: 'templateGrid',
                selModel: {
                    selType: 'checkboxmodel',
                    mode: 'SINGLE'
                },
                tools: [{
                    type: 'refresh',
                    tooltip: '刷新',
                    callback: function (grid) {
                        grid.getStore().reload();
                    }
                }],
                icon: 'resources/icons/16/application_view_list.png',
                store: Ext.create('Monkey.store.code.Template', {pageSize: 200}),
                listeners: {
                    rowclick: 'selectTemplate'
                },
                columns: [{
                    width: 20,
                    xtype: 'rownumberer'
                }, {
                    text: '模版名',
                    sortable: true,
                    flex: 1,
                    dataIndex: 'name'
                }, {
                    text: '类名',
                    dataIndex: 'className',
                    flex: 1,
                    sortable: true
                }, {
                    text: '预览图',
                    width: 80,
                    xtype: 'widgetcolumn',
                    widget: {
                        xtype: 'button',
                        text: '查看',
                        handler: 'showPicture'
                    }
                }]

            }, {
                flex: 2,
                reference: 'customParamConfig',
                title: '模版参数配置',
                icon: 'resources/icons/16/tag.png',
                layout: 'fit',
                bodyStyle: 'border-width:1px 0 0 0'
            }]
        };

        this.items = [
            modelPanel,
            tplPanel
        ];
        this.callParent();
    }
});